<template>
  <div class="index">
    <div class="navBar">
      <!-- <div class="back">
        <van-icon name="arrow-left" size="20" />
      </div> -->
      <div class="center">
        <van-search v-model="value" show-action :clearable="false" placeholder="请输入搜索关键词" @search="onSearch" >
          <template #action>
            <div class="search" @click="onSearch">搜索</div>
          </template>
        </van-search>
      </div>
    </div>
    
    <div class="list">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" :immediate-check="false" @load="onload" >
        <div class="item" v-for="(item, index) in list" :key="index" @click="goDetail(item)">
          <div class="leftImg ui-flex justify-center center">
            <div class="cell">
              <img :src="$config.ImgUrl+item.img_url" alt="">
            </div>
          </div>
          <div class="rightCenter">
            <div class="title">{{item.itemname}}</div>
            <div class="codeCenter">
              <div class="say">条码：{{item.itemcode}}</div>
              <div class="say">规格：{{item.frgnname}}</div>
            </div>
            <div class="tools">
              <div class="bd">保质期：{{item.u_freshday}}天</div>
              <div class="bd">库存：{{parseFloat(item.onhand)}}</div>
              <div class="bd">库位：{{item.u_placecode}}</div>
            </div>
          </div>
        </div>
      
      </van-list>
      <van-empty :description="empty" v-if="emptyShow" style="padding-top: 100px;" />
    </div>
   
  </div>
</template>

<script>
  import {icon, search, empty, list} from 'vant'
  export default {
    name: 'Home',
    components:{
      [icon.name]: icon,
      [search.name]: search,
      [empty.name]: empty,
      [list.name]: list
    },
    data(){
      return{
        value: '',
        empty: '请输入关键词搜索',
        emptyShow: true,
        list: [],
        page: 0,
        total: 0,
        loading: false,
        finished: false,
      }
    },
    mounted() {
      if(this.$route.query.keyword){
        this.value = this.$route.query.keyword;
        this.onSearch();
      }
    },
    methods:{
      onSearch(){
        this.emptyShow = false;
        this.list = [];
        this.page = 0;
        this.loading = false;
        this.finished = false;
        this.onload();
      },
      async onload(){
        this.loading = true;
        this.page = this.page + 1;
        var params = {
          keyword: this.value,
          page: this.page
        }
        var res = await this.http.get('/location/index/search',params);
        this.loading = false;
        var data = res.data;
        if(data.code == 200){
          this.total = data.data.total;
          this.list = [...this.list, ...data.data.data];
        } else {
          this.loading = false;
          this.finished = true;
          return;
        }
        
        if(this.list.length == 1){
          this.$router.push('/detail?code='+this.list[0].itemcode)
        }
        
        if(this.total == this.list.length){
          this.loading = false;
          this.finished = true;
        }
        
        // if(this.list.length){
        //   this.emptyShow = false;
        // } else{
        //   this.emptyShow = true;
        //   this.empty = '换个关键词试试'
        // }
        
      },
      goDetail(item){
        this.$router.push('/detail?code='+item.itemcode);
      }
    }
  }
</script>
<style lang="scss">
  .index{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
    .list{
      padding-top: 50px;
      overflow: auto;
      height: 100vh;
      .item{
        background-color: #FFFFFF;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        padding: 7px;
        .rightCenter{
          padding-left: 12px;
          height: 92px;
          flex: 1;
          .tools{
            display: flex;
            align-items: center;
            padding-top: 14px;
            .bd{
              flex: 1;
              font-size: 12px;
              color: #333333;
              text-align: left;
              &:first-child{
                width: 4%;
                flex: auto;
              }
            }
          }
          .codeCenter{
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding-top: 15px;
            .say{
              font-size: 12px;
              color: #333333;
            }
          }
          .title{
            font-size: 13px;
            color: #333333;
            padding-top: 6px;
            width: 200px;
            overflow: hidden;
            text-overflow:ellipsis;
            white-space: nowrap;
            text-align: left;
          }
        }
        .leftImg{
          width: 92px;
          height: 92px;
          background-color: #F5F5F5;
          overflow: hidden;
          img{
            width: 100%;
          }
        }
        &:first-child{
          margin-top: 2px;
        }
      }
    }
    .navBar{
      display: flex;
      align-items: center;
      padding: 0 14px;
      height: 50px;
      background-color: #FFFFFF;
      position: fixed;
      left: 0;
      top: 0;
      width: 100%;
      .van-field{
        padding: 0;
        display: flex;
        align-items: center;
      }
      .van-field__control{
        height: 30px;
      }
      .van-search__content{
        background-color: #F7F6F5;
      }
      .van-search__action{
        padding: 0;
      }
      .van-search{
        padding: 0;
      }
      .search{
        width: 62px;
        height: 30px;
        line-height: 30px;
        border-radius: 3px;
        background-color: #FF6400;
        color: #FFFFFF;
        font-size: 14px;
      }
      .center{
        flex: 1;
      }
    }
    div{
      box-sizing: border-box;
    }
  }
</style>
